﻿using System;
using System.Collections.Generic;
using System.Text;

namespace _02.EvenOccurrencesNumbers
{
    public class EvenOccurrencesNumbers
    {
        static void Main(string[] args)
        {
            //TestGenerator.CreateTest("test11.in.txt", "test11.out.txt"); return;

            //List<int> numbers = new List<int>( new int[] { 4, 2, 2, 5, 2, 3, 2, 3, 1, 5, 2, 6, 6, 6 });
            List<int> numbers = new List<int>(ReadConsoleInput());

            Dictionary<int, int> numberOccurTimesCounter = OccurrencesCounter(numbers.ToArray());

            //RemoveOddOccurrencesNumbers(numbers, numberOccurTimesCounter);
            numbers = EvenOccurrencesNumbersList(numbers, numberOccurTimesCounter);

            Print(numbers);
        }

        public static void RemoveOddOccurrencesNumbers(List<int> numbers, Dictionary<int, int> numberOccurTimesCounter)
        {
            foreach (var numberOccurTimes in numberOccurTimesCounter)
            {
                if (numberOccurTimes.Value % 2 == 1)
                {
                    //numbers.RemoveAll(x => x == numberOccurTimes.Key);
                    for (int i = 0; i < numberOccurTimes.Value; i++)
                    {
                        numbers.Remove(numberOccurTimes.Key);
                    }
                }
            }
        }

        public static List<int> EvenOccurrencesNumbersList(List<int> numbers, Dictionary<int, int> numberOccurTimesCounter)
        {
            List<int> evenOccurrencesNumbers = new List<int>();
            foreach (int number in numbers)
            {
                if (numberOccurTimesCounter[number] % 2 == 0)
                {
                    evenOccurrencesNumbers.Add(number);
                }
            }
            return evenOccurrencesNumbers;
        }

        private static void Print(List<int> numbers)
        {
            StringBuilder output = new StringBuilder();
            if (numbers.Count > 0)
            {
                for (int i = 0; i < numbers.Count - 1; i++)
                {
                    output.AppendFormat("{0} ", numbers[i]);
                }
                output.Append(numbers[numbers.Count - 1]);
            }
            Console.WriteLine(output);
        }

        private static int[] ReadConsoleInput()
        {
            string sequenceStr = Console.ReadLine();
            return ReadInput(sequenceStr);
        }

        public static int[] ReadInput(string sequenceStr)
        {
            string[] sequenceTokens = sequenceStr.Split(
                new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            int[] sequence = new int[sequenceTokens.Length];
            for (int i = 0; i < sequenceTokens.Length; i++)
            {
                sequence[i] = int.Parse(sequenceTokens[i]);
            }
            return sequence;
        }

        public static Dictionary<T, int> OccurrencesCounter<T>(T[] sequence)
        {
            Dictionary<T, int> оccurTimeCounter = new Dictionary<T, int>();

            for (int i = 0; i < sequence.Length; i++)
            {
                if (оccurTimeCounter.ContainsKey(sequence[i]))
                {
                    оccurTimeCounter[sequence[i]]++;
                }
                else
                {
                    оccurTimeCounter.Add(sequence[i], 1);
                }
            }
            return оccurTimeCounter;
        }
    }
}
